package com.ctgu.module.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ctgu.common.util.CoreConst;
import com.ctgu.module.admin.model.Email;
import com.ctgu.module.admin.model.User;
import com.ctgu.module.admin.service.*;
import lombok.AllArgsConstructor;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.baidubce.http.ApiExplorerClient;
import com.baidubce.http.AppSigner;
import com.baidubce.http.HttpMethodName;
import com.baidubce.model.ApiExplorerRequest;
import com.baidubce.model.ApiExplorerResponse;

import java.util.Map;

/**
 * 后台管理页面跳转控制器
 */
@Controller
@AllArgsConstructor
public class AdminRenderController {


    private final BizCategoryService categoryService;
    private final SysConfigService sysConfigService;
    private final PermissionService permissionService;
    private final BizStatisticService statisticService;
    private final EmailService emailService;
    /**
     * 薪火邮件
     */
    @RequestMapping("/admin")
    public String index(Model model) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if(user==null)
            return "redirect:/login";



        model.addAttribute("user",user);
        model.addAttribute("menuTree", permissionService.selectMenuTreeByUserId(((User) SecurityUtils.getSubject().getPrincipal()).getUserId()));
        return CoreConst.ADMIN_PREFIX + "index/index";
    }

    /**
     * 邮件主页天气
     */
    @PostMapping("/weather")
    @ResponseBody
    public String getWeather(){
        String path = "https://weatherquery.api.bdymkt.com/weather/query/by-area";
        ApiExplorerRequest request = new ApiExplorerRequest(HttpMethodName.POST, path);
        request.setCredentials("a3b658a001f6404e98a8f28e777bf16f", "21b2877913514636b7834f4b28731383");

        request.addHeaderParameter("Content-Type", "application/json;charset=UTF-8");

        request.addQueryParameter("area", "宜昌");
//        request.addQueryParameter("areaId", "");

        ApiExplorerClient client = new ApiExplorerClient(new AppSigner());

        try {
            ApiExplorerResponse response = client.sendRequest(request);
            // 返回结果格式为Json字符串
//            System.out.println(response.getResult());
            return response.getResult();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return path;
    }

    /**
     * 查看邮件内容入口
     */
    @GetMapping("/read/{id}")
    public String readEmailById(@PathVariable("id") Integer id, Model model) {
        System.out.println("准备阅读的ID："+id);
        QueryWrapper<Email> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        Email email = emailService.getOne(wrapper);
        model.addAttribute("email",email);
        return "/admin/email/read";
    }
    /**
     * 查看邮件内容入口
     */
    @GetMapping("/read")
    public String readEmail(Model model) {
        System.out.println("准备阅读邮件");

        QueryWrapper<Email> wrapper = new QueryWrapper<>();
        wrapper.eq("id", 33);
        Email email = emailService.getOne(wrapper);

        model.addAttribute("email",email);
        System.out.println(email);

        return "/admin/email/read";
    }

    @GetMapping("/readSend/{id}")
    public String readSendEmailById(@PathVariable("id") Integer id, Model model) {
        QueryWrapper<Email> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        Email email = emailService.getOne(wrapper);
        model.addAttribute("email",email);
        return "/admin/email/readSend";
    }


    /**
     * 工作台
     */
    @GetMapping("/workdest")
    public String workdest(Model model) {
        model.addAttribute("statistic", statisticService.indexStatistic());
        return CoreConst.ADMIN_PREFIX + "index/dashboard";
    }



    /**
     * 用户列表入口
     */
    @GetMapping("/users")
    public String userList() {
        return CoreConst.ADMIN_PREFIX + "user/list";
    }

    /**
     * 角色列表入口
     */
    @GetMapping("/roles")
    public String roleList() {
        return CoreConst.ADMIN_PREFIX + "role/list";
    }

    /**
     * 权限列表入口
     */
    @GetMapping("/permissions")
    public String permissionList() {
        return CoreConst.ADMIN_PREFIX + "permission/list";
    }

    /**
     * 在线用户入口
     */
    @GetMapping("/online/users")
    public String onlineUsers() {
        return CoreConst.ADMIN_PREFIX + "onlineUsers/list";
    }

    /**
     * 网站基本信息
     *
     * @param model
     */
    @GetMapping("/siteinfo")
    public String siteinfo(Model model) {
        Map<String, String> map = sysConfigService.selectAll();
        model.addAttribute("siteinfo", map);
        return CoreConst.ADMIN_PREFIX + "site/siteinfo";
    }

    /**
     * 友情链接
     */
    @GetMapping("/links")
    public String links() {
        return CoreConst.ADMIN_PREFIX + "link/list";
    }

    /**
     * 分类
     */
    @GetMapping("/categories")
    public String categories() {
        return CoreConst.ADMIN_PREFIX + "category/list";
    }

    /**
     * 标签
     */
    @GetMapping("/tags")
    public String tags() {
        return CoreConst.ADMIN_PREFIX + "tag/list";
    }



    /**
     * 评论
     */
    @GetMapping("/comments")
    public String comments() {
        return CoreConst.ADMIN_PREFIX + "comment/list";
    }

    /**
     * 主题
     */
    @GetMapping("themes")
    public String themes() {
        return CoreConst.ADMIN_PREFIX + "theme/list";
    }

}
